package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.article.IArticleClient;
import com.heima.model.article.dtos.ArticleListDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmStatistics;
import com.heima.model.wemedia.vos.WmStatisticsMoreVo;
import com.heima.model.wemedia.vos.WmStatisticsVo;
import com.heima.utils.thread.WeMediaThreadLocalUtil;
import com.heima.wemedia.mapper.WmStatisticsMapper;
import com.heima.wemedia.service.WmStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class WmStatisticsServiceImpl implements WmStatisticsService {

    @Autowired
    private IArticleClient articleClient;

    /**
     * 自媒体端,图文统计数量统计
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult dimension(Date beginDate, Date endDate) {

        //参数校验
        if (beginDate == null || endDate == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        //登录校验
        Integer userId = WeMediaThreadLocalUtil.get();
        if (userId == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        //查询所有符合条件的文章(远程调用)
        ArticleListDto articleListDto = new ArticleListDto();
        articleListDto.setAuthorId(userId);
        articleListDto.setBeginDate(beginDate);
        articleListDto.setEndDate(endDate);
        ResponseResult responseResult = articleClient.findByAuthIdAndDate(articleListDto);

        if (!responseResult.getCode().equals(200)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "远程调用查询文章错误");
        }

        //响应结果准备
        int likesNum = 0;
        int collectNum = 0;
        int publishNum = 0;

        //解析数据
        List<ApArticle> apArticles = JSON.parseArray(JSON.toJSONString(responseResult.getData()), ApArticle.class);
        if (apArticles != null || apArticles.size() > 0) {
            //遍历查询结果
            for (ApArticle apArticle : apArticles) {
                if (apArticle.getLikes() != null) {
                    likesNum += apArticle.getLikes();
                }
                if (apArticle.getCollection() != null) {
                    collectNum += apArticle.getCollection();
                }
            }
            publishNum = apArticles.size();
        }
        //结果封装
        WmStatisticsVo wmStatisticsVo = new WmStatisticsVo();
        wmStatisticsVo.setLikesNum(likesNum);
        wmStatisticsVo.setCollectNum(collectNum);
        wmStatisticsVo.setPublishNum(publishNum);
        //结果返回
        return ResponseResult.okResult(wmStatisticsVo);
    }

    /**
     * 图文统计,显示查询到的数据
     *
     * @param beginDate
     * @param endDate
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResponseResult newPage(Date beginDate, Date endDate, Integer page, Integer size) {

        //参数校验
        if (beginDate == null || endDate == null || page == null || size == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        //登录校验
        Integer userId = WeMediaThreadLocalUtil.get();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //远程调用,查询数据
        ArticleListDto articleListDto = new ArticleListDto();
        articleListDto.setAuthorId(userId);
        articleListDto.setBeginDate(beginDate);
        articleListDto.setEndDate(endDate);
        ResponseResult result = articleClient.findByAuthIdAndDate(articleListDto);

        //判断
        if (!result.getCode().equals(200)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "远程调用查询文章错误");
        }

        List<WmStatisticsMoreVo> list = new ArrayList<>();
        int total = 0;

        //结果解析
        List<ApArticle> apArticles = JSON.parseArray(JSON.toJSONString(result.getData()), ApArticle.class);

        //使用分页参数
        for (int i = (page - 1) * size; i < Math.min(page * size, apArticles.size()); i++) {
            //非空校验
            if (apArticles.get(i) != null) {
                //设置响应数据
                WmStatisticsMoreVo wmStatisticsMoreVo = new WmStatisticsMoreVo();
                wmStatisticsMoreVo.setId(apArticles.get(i).getId());
                if (apArticles.get(i).getTitle() != null)
                    wmStatisticsMoreVo.setTitle(apArticles.get(i).getTitle());
                wmStatisticsMoreVo.setViews(apArticles.get(i).getViews() == null ? 0 : apArticles.get(i).getViews());
                wmStatisticsMoreVo.setLikes(apArticles.get(i).getLikes() == null ? 0 : apArticles.get(i).getLikes());
                wmStatisticsMoreVo.setCollection(apArticles.get(i).getCollection() == null ? 0 : apArticles.get(i).getCollection());
                wmStatisticsMoreVo.setComment(apArticles.get(i).getComment() == null ? 0 : apArticles.get(i).getComment());
                list.add(wmStatisticsMoreVo);
            }
        }
        total = apArticles.size();


        //结果封装
        PageResponseResult responseResult = new PageResponseResult(page, size, total);
        responseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        responseResult.setData(list);
        responseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());

        return responseResult;
    }
}
